#!/usr/bin/env python
# Sample platypus document
# From the FAQ at reportlab.org/oss/rl-toolkit/faq/#1.1

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
import time

PAGE_HEIGHT=letter[1]
PAGE_WIDTH=letter[0]
styles = getSampleStyleSheet()
Ln1 = "A Little Luxury Consignment Boutique"
Ln2 = '7914 Memorial Parkway SW'
Ln3 = 'Huntsville, AL 35802'
Ln4 = '256-970-4117'
Ln5 = 'Sales Receipt'


lnspc16 = 20
lnspc9 = 16

pageinfo = time.asctime()

class docWriter():
    def __init__(self,subTotal,taxTotal, ovrTotal, purchase_list, receiptfile, tendered, change, pmtType):
        print purchase_list
        self.subTotal = subTotal
        self.taxTotal = taxTotal
        self.ovrTotal = ovrTotal
        self.purchase_list = purchase_list
        self.receiptfile = receiptfile
        self.tendered = tendered
        self.change = change
        self.pmtType = pmtType
        
    def myFirstPage(self, canvas, doc):
        canvas.saveState()
        canvas.setFont('Times-Bold',16)
        canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-80, Ln1)
        canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-80-lnspc16, Ln2)
        canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-80-lnspc16*2, Ln3)
        canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-80-lnspc16*3, Ln4)
        canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-80-lnspc16*4, Ln5)
        
        canvas.setFont('Times-Bold',12)
        
        startx = 1.0*inch
        starty = PAGE_HEIGHT - 80 - lnspc16*10
        for sale_item in reversed(self.purchase_list):
            booth,itemnumber,consignor,description,price = sale_item
            canvas.drawString(startx, starty, '%s' % (booth))
            canvas.drawString(startx+0.35*inch, starty, '%s' % (itemnumber))
            canvas.drawString(startx+0.8*inch, starty, description)
            canvas.drawString(startx+6*inch, starty, '$%.2f' % float(price))
            starty+=lnspc9

        canvas.drawString(startx+2*inch,2*inch, 'Payment Method : %s  Tendered : $%.2f  Change : $%.2f' % (self.pmtType, float(self.tendered), float(self.change)))            
        canvas.drawString(startx+2*inch,2*inch+lnspc9, 'Sub Total : $%.2f  Tax : $%.2f  Total : $%.2f' % (self.subTotal, self.taxTotal, self.ovrTotal))

        
        starty+=lnspc9
        
        
        canvas.drawString(inch, 0.75 * inch," Thank you for shopping A Little Luxury / All sales are final / %s" % pageinfo)
        canvas.restoreState()
        
    def myLaterPages(self, canvas, doc):
        pass
        #canvas.saveState()
        #canvas.setFont('Times-Roman', 9)
        #canvas.drawString(inch, 0.75 * inch,"Page %d %s" % (doc.page, pageinfo))
        #canvas.restoreState()
    
def start(subTotal,taxTotal,ovrTotal, purchase_list, receiptfile, tendered, change, pmtType):
    main = docWriter(subTotal,taxTotal,ovrTotal, purchase_list, receiptfile, tendered, change, pmtType)
    doc = SimpleDocTemplate(receiptfile)
    Story = [Spacer(1,2*inch)]
    style = styles["Normal"]
    #for i in range(100):
     #   bogustext = ("Paragraph number %s. " % i) *20
      #  p = Paragraph(bogustext, style)
       # Story.append(p)
        #Story.append(Spacer(1,0.2*inch))
    doc.build(Story, onFirstPage=main.myFirstPage, onLaterPages=main.myLaterPages)
    
if __name__ == "__main__":
    start(0,0,0,[[0,0,0,0,0],[0,0,0,0,0],[100,100,100,'Patridge, pear tree excluded',599.00],[0,0,0,'One expensive bag',0],[0,0,0,' One nice purse',0]],'./phello.pdf')
